package cn.maka.web.servlet;

import cn.maka.domain.PageBean;
import cn.maka.domain.User;
import cn.maka.service.UserService;
import cn.maka.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)){
            currentPage="1";
        }
        if (rows==null || "".equals(rows)){
            rows  = "5";
        }

        //用于条件查询，包含所有提交的条件，包含三个input窗口的name,返回的值是string[]数组类型，记得加[0]
        Map<String, String[]> condition = request.getParameterMap();


        UserService userService = new UserServiceImpl();
        PageBean<User> pageBean = userService.findUserByPage(currentPage,rows,condition);

        request.setAttribute("pageBean",pageBean);
        request.setAttribute("condition",condition);

//        System.out.println(pageBean);

        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
